package com.xbdun.web.bmp.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.constant.XbdunLoginConst;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.comweb.bmp.frame.annotation.XbdunLogin;
import com.xbdun.comweb.bmp.util.UserInfoUtil;
import com.xbdun.facade.bmp.constant.DubboBmpConst;
import com.xbdun.facade.bmp.request.system.DepartRequest;
import com.xbdun.facade.bmp.request.system.ModifyDepartRoleRequest;
import com.xbdun.facade.bmp.request.system.OrleDepartPagerRequest;
import com.xbdun.facade.bmp.request.system.OrleDepartRequest;
import com.xbdun.facade.bmp.request.system.RolePagerRequest;
import com.xbdun.facade.bmp.result.system.OrleDepartMenusResult;
import com.xbdun.facade.bmp.result.system.OrleDepartPagerResult;
import com.xbdun.facade.bmp.result.system.RolePagerResult;
import com.xbdun.facade.bmp.result.system.TreeNodeResult;
import com.xbdun.facade.bmp.service.system.DepartFacade;
import com.xbdun.facade.bmp.service.system.OrleDepartFacade;
import com.xbdun.facade.bmp.service.system.PowerRoleDepartFacade;
import com.xbdun.facade.bmp.service.system.RoleFacade;
import com.xbdun.facade.bmp.service.system.SysMenuFacade;

import lombok.extern.slf4j.Slf4j;

/**
 * 企业部门
 * @author  wwq
 *
 */
@Controller
@Slf4j
@XbdunLogin({XbdunLoginConst.XBDADMINT,XbdunLoginConst.ADMINS})
public class S100101102Controller extends BaseSystemController{
	@Reference(registry=DubboBmpConst.REGISTRYS)
	DepartFacade departFacade;
	@Reference(registry=DubboBmpConst.REGISTRYS)
	RoleFacade roleFacade;
	@Reference(registry=DubboBmpConst.REGISTRYS)
	OrleDepartFacade orleDepartFacade;
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysMenuFacade sysMenuFacade;
	@Reference(registry=DubboBmpConst.REGISTRYS)
	PowerRoleDepartFacade powerRoleDepartFacade;
	
	/**
	 * 企业部门主页
	 * @return
	 */
	@RequestMapping("100101102")
    public String index(){
        return ROOT_PATH+"100101102";
    }
	
	/**
	 * 获取部门树
	 * @return
	 */
	@RequestMapping("100101102106")
	@ResponseBody
	public TreeNodeResult getTree(){
		TreeNodeResult result = new TreeNodeResult();
		try{
			result = departFacade.queryTreeDepart(UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取部门树异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 获取当前部门角色已有角色
	 * @param request
	 * @return
	 */
	@RequestMapping("100101102106X")
	@ResponseBody
	public OrleDepartPagerResult getDepartRoleList(OrleDepartPagerRequest request){
		OrleDepartPagerResult result = new OrleDepartPagerResult();
		try{
			log.info("获取当前部门角色传入参数,{}",request);
			result = orleDepartFacade.queryOrleDepartOrg(request, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取当前部门角色异常,{}", e);
		}
		return result;
	}
	
	/**
	 * 新增部门信息
	 * @param request
	 * @return
	 */
	@RequestMapping("100101102100")
	@ResponseBody
	public XbdUnStringResult addDepart(DepartRequest request){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("新增部门信息传入参数,{}", request);
			result = departFacade.addDepart(request, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("新增部门信息异常,{}", e);
		}
		return result;
	}
	
	/**
	 * 修改部门信息
	 * @param request
	 * @return
	 */
	@RequestMapping("100101102101")
	@ResponseBody
	public XbdUnStringResult modifyDepart(DepartRequest request){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("修改部门传入参数,{}", request);
			result = departFacade.modifyDepart(request, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("修改部门异常,{}", e);
		}
		return result;
	}
	
	/**
	 * 删除部门
	 * @param request
	 * @return
	 */
	@RequestMapping("100101102102")
	@ResponseBody
	public XbdUnStringResult deleteDepart(Long id){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("删除部门传入参数,{}", id);
			result = departFacade.delDepartByCode(id, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("删除部门异常,{}", e);
		}
		return result;
	}
	
	/**
	 * 获取当前机构所有角色
	 * @param request
	 * @return
	 */
	@RequestMapping("100101102103")
	@ResponseBody
	public RolePagerResult getOrgRoleList(RolePagerRequest request){
		RolePagerResult result = new RolePagerResult();
		try{
			result = roleFacade.queryPager(request, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取当前机构所有角色,{}", e);
		}
		return result;
	}
	
	/**
	 * 新增部门的角色
	 * @param request
	 * @return
	 */
	@RequestMapping("100101102103X")
	@ResponseBody
	public XbdUnStringResult addDepartRoles(OrleDepartRequest request,String roleIdStr){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("新增部门的角色,{}",request,roleIdStr);
			if (roleIdStr != null){
				String[] roleIds = roleIdStr.split(",");
				if (roleIds.length != 0) {
					List<Long> ids = new ArrayList<Long>();
					for (int i = 0; i < roleIds.length; i ++){
						ids.add(Long.valueOf(roleIds[i]));
					}
					request.setRoleId(ids);
				}
				result = orleDepartFacade.addDepartRoles(request, UserInfoUtil.getClient(getRequest()));
			}
		}catch(Exception e){
			result.setBizException(e);
			log.error("新增部门的角色,{}", e);
		}
		return result;
	}
	
	/**
	 * 删除部门下的角色
	 * @param request
	 * @return
	 */
	@RequestMapping("100101102105")
	@ResponseBody
	public XbdUnStringResult delDepartRole(Long id){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("删除部门下的角色,{}",id);
			result = orleDepartFacade.delDepartRole(id, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("删除部门下的角色,{}", e);
		}
		return result;
	}
	
	/**
	 * 设置权限======获取当前机构所有菜单树
	 * @return
	 */
	@RequestMapping("100101102104")
	@ResponseBody
	public TreeNodeResult getMenuTree(){
		TreeNodeResult result = new TreeNodeResult();
		try{
			result = sysMenuFacade.queryGrantMenu(UserInfoUtil.getClient(getRequest()).getOrgCode(), UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取当前机构菜单树异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 设置权限======获取当前角色菜单树
	 * @return
	 */
	@RequestMapping("100101102104X")
	@ResponseBody
	public OrleDepartMenusResult queryOrleDepartMenus(Long orleDepartId){
		OrleDepartMenusResult result = new OrleDepartMenusResult();
		try{
			result = powerRoleDepartFacade.queryOrleDepartMenus(orleDepartId, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取当前机构菜单树异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 给部门下的角色授权菜单
	 * @return
	 */
	@RequestMapping("100101102104XX")
	@ResponseBody
	public XbdUnStringResult modifyDepartRole(Long orleDepartId,String addMenuCodeStr,String delMenuCodeStr){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			if (orleDepartId == null||orleDepartId.longValue()<0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("部门角色中间表ID不合法");
			}
			ModifyDepartRoleRequest request = new ModifyDepartRoleRequest();
			request.setOrleDepartId(orleDepartId);
			
			boolean bl = false;
			if (addMenuCodeStr != null && !addMenuCodeStr.equals("")){
				String[] addCodes = addMenuCodeStr.split(",");
				if (addCodes[0] != "") {
					List<Long> addMenuCode = new ArrayList<Long>();
					for (int i = 0; i < addCodes.length; i ++){
						addMenuCode.add(Long.valueOf(addCodes[i]));
					}
					bl = true;
					request.setAddMenuCode(addMenuCode);
				}
			}
			if (delMenuCodeStr != null && !delMenuCodeStr.equals("")){
				String[] cancelCode = delMenuCodeStr.split(",");
				if (cancelCode[0] != "") {
					List<Long> delMenuCode = new ArrayList<Long>();
					for (int i = 0; i < cancelCode.length; i ++){
						delMenuCode.add(Long.valueOf(cancelCode[i]));
					}
					bl = true;
					request.setDelMenuCode(delMenuCode);;
				}
			}
			log.info("给部门下的角色授权菜单请求参数:{}", request);
			if (bl) {
				result = powerRoleDepartFacade.modifyDepartRole(request,UserInfoUtil.getClient(getRequest()));
			} else {
				throw BizException.PARAM_CHKE_FAIL.newInstance("未进行任何修改!");
			}
		}catch(Exception e){
			result.setBizException(e);
			log.error("给部门下的角色授权菜单异常:{}", e);
		}
		return result;
	}
	
	
	
}
